<template>
  <div>
    <el-button
      type="primary"
      @click="isAddService = true"
      v-show="activeName == 0 && isPermissionsBtn('store_service_add')"
      icon="el-icon-circle-plus-outline"
    >新增服务项</el-button>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="服务文本" name="0">
        <div class="table-body">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column type="index" :index="indexMethod" width="80" label="序号"></el-table-column>
            <el-table-column prop="serviceName" label="服务项标题"></el-table-column>
            <el-table-column prop="description" label="服务项内容"></el-table-column>
            <el-table-column fixed="right" width="180" label="操作">
              <template slot-scope="scope">
                <el-button
                  @click="update(scope.row)"
                  v-if="isPermissionsBtn('store_service_update')"
                  size="mini"
                  type="primary"
                >修改</el-button>
                <el-popconfirm title="确定删除此项服务文本？" @onConfirm="handleConfirmDelete(scope.row)">
                  <el-button
                    style="margin-left:20px"
                    size="mini"
                    type="danger"
                    v-if="isPermissionsBtn('store_service_del')"
                    slot="reference"
                  >删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-tab-pane>
      <el-tab-pane label="热销商品" name="2">
        <div class="table-body">
          <el-table :data="tableData2" style="width: 100%" border>
            <el-table-column type="index" width="80" label="序号"></el-table-column>
            <el-table-column width="600" label="商品">
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.cid1"
                  style="width:32%"
                  placeholder="请选择"
                  @change="fristGoodClick(scope.row,scope.$index)"
                >
                  <el-option
                    v-for="item in productOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
                <el-select
                  v-model="scope.row.cid2"
                  style="width:32%;margin-left:1%"
                  placeholder="请选择"
                  @change="secondGoodClick(scope.row,scope.$index)"
                >
                  <el-option
                    v-for="item in scope.row.getList2"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
                <el-select
                  v-model="scope.row.id"
                  style="width:32%;margin-left:1%"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in scope.row.getList3"
                    :key="item.id"
                    :label="item.title"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column prop="setTime" label="设置时间"></el-table-column>
          </el-table>
        </div>
        <div style="text-align:center; margin-top: 50px;">
          <el-button
            type="primary"
            @click="hotSubmitForm()"
            v-if="isPermissionsBtn('store_hot_add')"
          >保存</el-button>
        </div>
      </el-tab-pane>
      <!-- <el-tab-pane label="线下服务"
                         name="1">
                <div class="table-body">
                    <el-table :data="tableData1"
                              style="width: 100%">
                        <el-table-column type="index"
                                         :index="indexMethod"
                                         width="180"
                                         label="序号"></el-table-column>
                        <el-table-column prop="storeName"
                                         width="180"
                                         label="门店名称"></el-table-column>
                        <el-table-column prop="storeAddress"
                                         label="门店地址"></el-table-column>
                        <el-table-column fixed="right"
                                         width="180"
                                         label="操作">
                            <template slot-scope="scope">
                                <el-button @click="update1(scope.row)"
                                           size="mini">修改</el-button>
                                <el-popconfirm title="确定删除此门店？"
                                               @onConfirm="handleConfirmDelete1(scope.row)">
                                    <el-button style="margin-left:20px"
                                               size="mini"
                                               type="danger"
                                               slot="reference">删除</el-button>
                                </el-popconfirm>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
      </el-tab-pane>-->
    </el-tabs>

    <el-dialog title="新增服务项" :visible.sync="isAddService" width="30%">
      <AddService @submit="submitevent" />
    </el-dialog>
    <el-dialog title="修改服务项" :visible.sync="isupdateService" width="30%">
      <updateservice
        @submit="updateEvent"
        :data="servicerow"
        @cancel="isupdateService = $event"
        v-if="isupdateService"
      />
    </el-dialog>
    <el-dialog title="新增门店" :visible.sync="isAddStore" width="30%">
      <AddStore @submit="submitevent1" />
    </el-dialog>
    <el-dialog title="修改门店" :visible.sync="isupdateStore" width="30%">
      <updateStore
        @submit="updateEvent1"
        :data="storeRow"
        @cancel="isupdateStore = $event"
        v-if="isupdateStore"
      />
    </el-dialog>
  </div>
</template>

<script>
import AddService from "./AddService";
import AddStore from "./AddStore";
import updateservice from "./updateService";
import updateStore from "./updateStore";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      activeName: 0,
      isAddService: false,
      isAddStore: false,
      isupdateService: false,
      isupdateStore: false,
      servicerow: {},
      storeRow: {},
      tableData: [],
      tableData1: [],
      tableData2: [],
      productOptions: []
    };
  },
  mounted() {
    this.getserviceList();
    this.goodFirst();
    this.initData();
  },
  computed: {
    ...mapGetters(["isPermissionsBtn"])
  },
  methods: {
    goodFirst() {
      this.$service.firstGood().then(res => {
        if (res.code == "00000") {
          this.productOptions = res.data || [];
        }
      });
    },
    fristGoodClick(val, index) {
      val.cid2 = "";
      val.getList2 = [];
      val.id = "";
      val.getList3 = [];
      this.goodSecond(val, index);
    },
    goodSecond(val, index) {
      this.$service.secondGood(val.cid1).then(res => {
        if (res.code == "00000") {
          val.getList2 = res.data || [];
        }
      });
    },
    secondGoodClick(val, index) {
      val.id = "";
      val.getList3 = [];
      this.goodThird(val, index);
    },
    goodThird(val, index) {
      var param = {
        tbSpu: {
          cid2: val.cid2
        }
      };
      this.$service.getCommodityCategory(param).then(res => {
        if (res.code == "00000") {
          val.getList3 = res.data || [];
        }
      });
    },
    hotSubmitForm() {
      var param = {
        tbSpuList: []
      };
      for (let i = 0; i < this.tableData2.length; i++) {
        var item = this.tableData2[i];
        var dic = {
          id: item.id
        };
        param.tbSpuList.push(dic);
      }
      console.log(param);
      this.$service.tbSpuUpdateIsHot(param).then(res => {
        if (res.code == "Z0000") {
          this.$message({
            message: "提交成功",
            type: "success"
          });
          this.initData();
        } else {
          this.$message({
            message: res.message,
            type: "warning"
          });
        }
      });
    },
    initData() {
      this.$service.tbSpuSelectIsHot().then(res => {
        if (res.code == "00000") {
          this.tableData2 = res.data;
          let n = 5 - res.data.length;
          for (let i = 0; i < n; i++) {
            var dic = {
              id: "",
              cid1: "",
              cid2: "",
              getList2: "",
              getList3: ""
            };
            this.tableData2.push(dic);
          }
        }
      });
    },
    handleChange() {},
    getserviceList() {
      this.$service.getServiceList().then(res => {
        //console.log(res);
        this.tableData = res.data;
      });
    },
    getStore() {
      this.$service.getStoreList().then(res => {
        console.log(res);
        this.tableData1 = res.data;
      });
    },
    handleConfirmDelete(row) {
      this.$service.deleteService(row.id).then(res => {
        this.$message({
          message: "删除成功",
          type: "success"
        });
        this.getserviceList();
      });
    },
    handleConfirmDelete1(row) {
      this.$service
        .deleteStore({
          id: row.id
        })
        .then(res => {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.getStore();
        });
    },
    handleClick(tab, event) {
      //console.log(tab, event);
      if (tab.index == 0) {
        this.getserviceList();
      } else {
        this.initData();
      }
    },
    update(row) {
      //console.log(row);
      this.servicerow = JSON.parse(JSON.stringify(row));
      this.isupdateService = true;
    },
    update1(row) {
      //console.log(row);
      this.storeRow = JSON.parse(JSON.stringify(row));
      this.isupdateStore = true;
    },
    indexMethod(index) {
      var index1 = index + 1;
      return index1++;
    },
    submitevent() {
      this.isAddService = false;
      this.getserviceList();
    },
    submitevent1() {
      this.isAddStore = false;
      this.getStore();
    },
    updateEvent() {
      this.isupdateService = false;
      this.getserviceList();
    },
    updateEvent1() {
      this.isupdateStore = false;
      this.getStore();
    }
  },
  components: {
    AddService,
    AddStore,
    updateservice,
    updateStore
  }
};
</script>

<style scoped>
.table-body {
  margin: 0;
}
</style>